﻿#include "zyLayoutBox.h"

// 构造函数：初始化布局框属性
DarkMoonUI::zyLayoutBox::zyLayoutBox()
{
    SetMember(L"this", (LONG_PTR)this);          // 设置当前对象指针
    m_obj->setWndProc(zyLayoutBox_WndProc);      // 设置窗口处理函数
    m_obj->setDestroy(zyLayoutBox_Destroy);      // 设置销毁回调
    SetMember(L"LayoutMode", 线性布局模式_垂直); // 默认垂直布局模式
}

// 析构函数
DarkMoonUI::zyLayoutBox::~zyLayoutBox() {}

// 窗口消息处理函数
LRESULT WINAPI DarkMoonUI::zyLayoutBox::zyLayoutBox_WndProc(
    zyObject* obj,HWND hWnd, UINT uMsg,WPARAM wParam, LPARAM lParam, BOOL* bHandled)
{
    switch (uMsg) {
    case zyLYM_LAYOUT:    // 布局请求
        reinterpret_cast<zyLayoutBox*>(obj->getPtrData(L"this"))->zyLayoutBox_Layout(obj);
        return 1;
    case zyLYM_GETMINSIZE: // 获取最小尺寸
        return reinterpret_cast<zyLayoutBox*>(obj->getPtrData(L"this"))->zyLayoutBox_GetMinSize(obj);
    }
    return zyLayout::zyLayout_WndProc(obj, hWnd, uMsg, wParam, lParam, bHandled);
}

// 销毁回调：释放资源
void WINAPI DarkMoonUI::zyLayoutBox::zyLayoutBox_Destroy(zyObject* obj)
{
    // 调用基类销毁方法
    zyLayout::zyBase_Destroy(obj);
}

// 获取布局最小尺寸
int DarkMoonUI::zyLayoutBox::zyLayoutBox_GetMinSize(zyObject* obj)
{
    // 获取用户定义的最小尺寸
    int minWidth = (int)(LONG_PTR)(obj->getPtrData(L"minWidth"));
    int minHeight = (int)(LONG_PTR)(obj->getPtrData(L"minHeight"));

    // 如果已明确定义则直接返回
    if (minWidth != -1 && minHeight != -1)
        return MAKELONG(minWidth, minHeight);

    // 自动计算最小尺寸逻辑
    auto pItems = static_cast<std::vector<zyLayoutItem>*>(obj->getPtrData(L"LayoutItems"));
    if (!pItems || pItems->empty())
        return MAKELONG(0, 0);

    const int layoutMode = (int)(LONG_PTR)(obj->getPtrData(L"LayoutMode"));
    int calcWidth = 0, calcHeight = 0;

    for (const auto& item : *pItems) {
        // 获取子项实际最小尺寸
        int itemWidth = item.minWidth;
        int itemHeight = item.minHeight;

        // 如果是嵌套布局则递归获取
        if (item.pObject && (int)(LONG_PTR)item.pObject->getPtrData(L"Type") == zyLayoutType_Layout) {
            LRESULT minSize = item.pObject->callWndProc(0, zyLYM_GETMINSIZE, 0, 0, nullptr);
            itemWidth = LOWORD(minSize);
            itemHeight = HIWORD(minSize);
        }

        // 根据布局方向累加尺寸
        const RECT& margin = *item.pMargin;
        if (layoutMode == 线性布局模式_水平) {
            calcWidth += itemWidth + margin.left + margin.right;
            calcHeight = max(calcHeight, itemHeight + margin.top + margin.bottom);
        }
        else {
            calcHeight += itemHeight + margin.top + margin.bottom;
            calcWidth = max(calcWidth, itemWidth + margin.left + margin.right);
        }
    }

    // 组合计算结果（考虑用户定义的最小值）
    return MAKELONG(
        minWidth == -1 ? calcWidth : max(minWidth, calcWidth),
        minHeight == -1 ? calcHeight : max(minHeight, calcHeight)
    );
}

// 执行布局计算
void DarkMoonUI::zyLayoutBox::zyLayoutBox_Layout(zyObject* obj)
{
    auto pItems = static_cast<std::vector<zyLayoutItem>*>(obj->getPtrData(L"LayoutItems"));
    if (!pItems || pItems->empty()) return;

    // 获取布局参数
    const RECT* pRect = static_cast<RECT*>(obj->getPtrData(L"pRect"));
    const int layoutMode = (int)(LONG_PTR)(obj->getPtrData(L"LayoutMode"));
    int position = 0; // 当前布局位置

    for (auto& item : *pItems) {
        // 计算当前项布局区域
        RECT itemArea = { 0 };
        const RECT& margin = *item.pMargin;
        const int itemWidth = item.pRect->right - item.pRect->left;
        const int itemHeight = item.pRect->bottom - item.pRect->top;

        if (layoutMode == 线性布局模式_水平) {
            itemArea = {
                pRect->left + position + margin.left,
                pRect->top + margin.top,
                pRect->left + position + margin.left + itemWidth,
                pRect->bottom - margin.bottom
            };
            position += itemWidth + margin.left + margin.right;
        }
        else {
            itemArea = {
                pRect->left + margin.left,
                pRect->top + position + margin.top,
                pRect->right - margin.right,
                pRect->top + position + margin.top + itemHeight
            };
            position += itemHeight + margin.top + margin.bottom;
        }

        // 计算对齐后的实际位置
        RECT targetRect = zyGetAlignRectI(
            itemArea,
            item.align,
            itemWidth,
            itemHeight,
            margin.left, margin.top, margin.right, margin.bottom
        );

        // 应用最小尺寸限制
        targetRect.right = max(targetRect.left + item.minWidth, targetRect.right);
        targetRect.bottom = max(targetRect.top + item.minHeight, targetRect.bottom);

        // 更新子项矩形
        *item.pRect = targetRect;

        // 递归布局子项或移动窗口
        if (item.pObject) {
            if ((int)(LONG_PTR)item.pObject->getPtrData(L"Type") == zyLayoutType_Layout) {
                // 处理嵌套布局
                RECT* childRect = static_cast<RECT*>(item.pObject->getPtrData(L"pRect"));
                *childRect = targetRect;
                item.pObject->callWndProc(0, zyLYM_LAYOUT, 0, 0, nullptr);
            }
            else {
                // 移动实际窗口
                HWND hWnd = static_cast<HWND>(item.pObject->getPtrData(L"hWnd"));
                MoveWindow(hWnd,
                    targetRect.left, targetRect.top,
                    targetRect.right - targetRect.left,
                    targetRect.bottom - targetRect.top,
                    TRUE
                );
            }
        }
    }
}

// 设置/获取布局模式
int DarkMoonUI::zyLayoutBox::模式(线性布局模式 参数_模式)
{
    const int current = static_cast<int>(GetMember(L"LayoutMode"));
    if (参数_模式 != -1) {
        SetMember(L"LayoutMode", 参数_模式);
    }
    return current;
}